package ch.supsi.mobile.core.applications;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

import ch.supsi.mobile.core.IAddress;
import ch.supsi.parentalcontrol.UserPanel;

public class AddressesApplication implements IAddressesApplication{

	//List<IAddress> whiteAddress = new ArrayList<IAddress>();
	//List<IAddress> blackAddress = new ArrayList<IAddress>();

	DefaultListModel wModel = new DefaultListModel();
	DefaultListModel bModel = new DefaultListModel();
	DefaultListModel cModel = new DefaultListModel();
	
	JList whiteAddress = new JList(wModel);
	JList blackAddress = new JList(bModel);
	
	public AddressesApplication(){
		
		rebuildList();
		
		wModel.addListDataListener(new ListDataListener() {
			
			@Override
			public void intervalRemoved(ListDataEvent e) {
				rebuildList();
			}
			
			@Override
			public void intervalAdded(ListDataEvent e) {
				rebuildList();
			}
			
			@Override
			public void contentsChanged(ListDataEvent arg0) {
				rebuildList();
			}
		});
		
		bModel.addListDataListener(new ListDataListener() {
			
			@Override
			public void intervalRemoved(ListDataEvent e) {
				rebuildList();
			}
			
			@Override
			public void intervalAdded(ListDataEvent e) {
				rebuildList();
			}
			
			@Override
			public void contentsChanged(ListDataEvent arg0) {
				rebuildList();
			}
		});
		
	}
	
	private void rebuildList(){
		cModel.removeAllElements();

		for(int i=0; i<wModel.size(); i++)
			cModel.addElement(wModel.get(i));
		for(int i=0; i<bModel.size(); i++)
			cModel.addElement(bModel.get(i));
	}
	
	public DefaultListModel getWhiteListModel(){
		return this.wModel;
	}
	
	public DefaultListModel getBlackListModel(){
		return this.bModel;
	}
	
	public DefaultListModel getCompleteListModel(){
		return this.cModel;
	}
	
	@Override
	public void addNewAddress(IAddress address) {
		addWhiteAddress(address);
	}

	private void addWhiteAddress(IAddress address){
		//whiteAddress.add(address);
		wModel.addElement(address);
	}
	
	public void addBlackAddress(IAddress address){
		//blackAddress.add(address);
		bModel.addElement(address);
	}
	
	@Override
	public void deleteAddress(IAddress address) {
		deleteWhiteAddress(address);
	}

	private void deleteWhiteAddress(IAddress address){
		//whiteAddress.remove(address);
		wModel.removeElement(address);
	}
	
	public void deleteBlackAddress(IAddress address){
		//blackAddress.remove(address);
		bModel.removeElement(address);
	}
	
	@Override
	public IAddress[] getAllAddresses() {
		return getWhiteAddresses();
	}

	private IAddress[] getWhiteAddresses(){
		/*IAddress[] listAddress = new IAddress[whiteAddress.size()];
		
		for(int i=0; i<whiteAddress.size(); i++){
			listAddress[i] = whiteAddress.get(i);
		}
		*/
		
		IAddress[] listAddress = new IAddress[wModel.getSize()];
		
		for(int i=0; i<wModel.getSize(); i++)
			listAddress[i] = (IAddress)wModel.getElementAt(i);
		
		return listAddress;
		
		
	}
	
	public IAddress[] getBlackAddresses(){
		/*IAddress[] listAddress = new IAddress[blackAddress.size()];
		
		for(int i=0; i<blackAddress.size(); i++){
			listAddress[i] = blackAddress.get(i);
		}*/

		IAddress[] listAddress = new IAddress[bModel.getSize()];
		
		for(int i=0; i<bModel.getSize(); i++)
			listAddress[i] = (IAddress)bModel.getElementAt(i);
		
		return listAddress;
	}
	
	@Override
	public IAddress searchAddressByName(String name) {
		return null;
	}

	@Override
	public IAddress searchAddressByNumber(String number) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IAddress searchAddressByPrefix(String prefix) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void updateAddress(IAddress address) {
		// TODO Auto-generated method stub
		
	}

}
